package com.gx.smart.building;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.AddWorkOrderEventReq;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.CountStatusQuantityReq;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.CountStatusQuantityResp;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.DeleteWorkOrderReq;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.InsertWorkOrderReq;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.InsertWorkOrderResp;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.QueryWorkOrderDetailReq;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.QueryWorkOrderDetailResp;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.QueryWorkOrderListReq;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.QueryWorkOrderListResp;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.UpdateWorkOrderReq;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.WorkOrderApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.WorkOrderResp;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderDto;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderEventDto;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderEventFileDto;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderFileDto;
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderPreAssetDto;
import com.orhanobut.logger.Logger;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/3/23
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class WorkOrderApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final WorkOrderApiService INSTANCE = new WorkOrderApiService();
    }

    public static WorkOrderApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private WorkOrderApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private WorkOrderApiGrpc.WorkOrderApiBlockingStub getStub(ManagedChannel channel) {
        return WorkOrderApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 查询工单列表
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryWorkOrderListResp> queryWorkOrderList(
            ComQuery comQuery,
            List<String> status,
            CallBack<QueryWorkOrderListResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryWorkOrderListResp>(callBack) {
            @Override
            protected QueryWorkOrderListResp doRequestData(ManagedChannel channel) {
                QueryWorkOrderListReq message = QueryWorkOrderListReq.newBuilder()
                        .setComQuery(comQuery)
                        .addAllStatus(status)
                        .build();
                Logger.d(message);
                QueryWorkOrderListResp response = null;
                try {
                    response = getStub(channel).queryWorkOrderList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryWorkOrderList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 查询工单列表
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryWorkOrderListResp> queryWorkOrderList2(
            ComQuery comQuery,
            String userId,
            CallBack<QueryWorkOrderListResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryWorkOrderListResp>(callBack) {
            @Override
            protected QueryWorkOrderListResp doRequestData(ManagedChannel channel) {
                QueryWorkOrderListReq message = QueryWorkOrderListReq.newBuilder()
                        .setComQuery(comQuery)
                        .setWorkOrderDto(WorkOrderDto.newBuilder()
                                .setOrderSourceId("698494489553608704")
                                .setReleaseUserId(userId)
                                .setCreateBy(userId).build())
                        .build();
                Logger.d(message);
                QueryWorkOrderListResp response = null;
                try {
                    response = getStub(channel).queryWorkOrderList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryWorkOrderList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }


    /**
     * 查看工单详情
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryWorkOrderDetailResp> queryWorkOrderDetail(
            String id,
            CallBack<QueryWorkOrderDetailResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryWorkOrderDetailResp>(callBack) {
            @Override
            protected QueryWorkOrderDetailResp doRequestData(ManagedChannel channel) {
                QueryWorkOrderDetailReq message = QueryWorkOrderDetailReq.newBuilder()
                        .setId(id)
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                QueryWorkOrderDetailResp response = null;
                try {
                    response = getStub(channel).queryWorkOrderDetail(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryWorkOrderDetail");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 新增工单
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, InsertWorkOrderResp> insertWorkOrder(
            String orderTypeId,
            String orderTypeCode,
            String orderTypeName,
            String orderSubTypeId,
            String orderSubTypeCode,
            String orderSubTypeName,
            String areaName,
            String description,
            String releaseId,
            String releaseUserName,
            String releasePhone,
            List<WorkOrderFileDto> files,
            String assetId,
            String assetName,
            CallBack<InsertWorkOrderResp> callBack) {
        return new GrpcAsyncTask<String, Void, InsertWorkOrderResp>(callBack) {
            @Override
            protected InsertWorkOrderResp doRequestData(ManagedChannel channel) {
                InsertWorkOrderReq message = InsertWorkOrderReq.newBuilder()
                        .setWorkOrderDto(WorkOrderDto
                                .newBuilder()
                                .setTenantNo(1)
                                .setOrderTypeId(orderTypeId)
                                .setOrderTypeCode(orderTypeCode)
                                .setOrderTypeName(orderTypeName)
                                .setStatus("task_assign")
                                .setOrderSubTypeId(orderSubTypeId)
                                .setOrderSubTypeCode(orderSubTypeCode)
                                .setOrderSubTypeName(orderSubTypeName)
                                .setAreaName(areaName)
                                .setDescription(description)
                                .setOrderSourceName("移动端")
                                .setOrderSourceId("698494489553608704")
                                .setOrderSourceCode("task_app")
                                .setReleaseUserId(releaseId)
                                .setReleaseUserName(releaseUserName)
                                .setReleaseUserPhone(releasePhone)
                                .addAllWorkOrderFileDto(files)
                                .setAssetId(assetId)
                                .setAssetName(assetName)
                                .build())
                        .build();
                Logger.d(message);
                InsertWorkOrderResp response = null;
                try {
                    response = getStub(channel).insertWorkOrder(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "InsertWorkOrderReq");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 修改工单
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, WorkOrderResp> updateWorkOrder(CallBack<WorkOrderResp> callBack) {
        return new GrpcAsyncTask<String, Void, WorkOrderResp>(callBack) {
            @Override
            protected WorkOrderResp doRequestData(ManagedChannel channel) {
                UpdateWorkOrderReq message = UpdateWorkOrderReq.newBuilder()
                        .build();
                Logger.d(message);
                WorkOrderResp response = null;
                try {
                    response = getStub(channel).updateWorkOrder(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "updateWorkOrder");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 逻辑删除工单
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, WorkOrderResp> deleteWorkOrder(CallBack<WorkOrderResp> callBack) {
        return new GrpcAsyncTask<String, Void, WorkOrderResp>(callBack) {
            @Override
            protected WorkOrderResp doRequestData(ManagedChannel channel) {
                DeleteWorkOrderReq message = DeleteWorkOrderReq.newBuilder()
                        .build();
                Logger.d(message);
                WorkOrderResp response = null;
                try {
                    response = getStub(channel).deleteWorkOrder(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "deleteWorkOrder");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 接单
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, WorkOrderResp> receiveTask(
            String orderId,
            String opinion,
            CallBack<WorkOrderResp> callBack) {
        return new GrpcAsyncTask<String, Void, WorkOrderResp>(callBack) {
            @Override
            protected WorkOrderResp doRequestData(ManagedChannel channel) {
                AddWorkOrderEventReq message = AddWorkOrderEventReq.newBuilder()
                        .setWorkOrderEventDto(WorkOrderEventDto.newBuilder()
                                .setWorkOrderId(orderId)
                                .setOpinion(opinion)
                                .build())
                        .build();
                Logger.d(message);
                WorkOrderResp response = null;
                try {
                    response = getStub(channel).receiveTask(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "receiveTask");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 处理
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, WorkOrderResp> processTask(
            String orderId,
            String opinion,
            List<WorkOrderEventFileDto> files,
            String assetId,
            String assetName,
            List<WorkOrderPreAssetDto> assetDtos,
            CallBack<WorkOrderResp> callBack) {
        return new GrpcAsyncTask<String, Void, WorkOrderResp>(callBack) {
            @Override
            protected WorkOrderResp doRequestData(ManagedChannel channel) {
                AddWorkOrderEventReq message = AddWorkOrderEventReq.newBuilder()
                        .setWorkOrderDto(WorkOrderDto.newBuilder()
                                .setAssetId(assetId)
                                .setAssetName(assetName)
                                .addAllWorkOrderPreAssetDto(assetDtos).build())
                        .setWorkOrderEventDto(WorkOrderEventDto.newBuilder()
                                .addAllWorkOrderEventFileDto(files)
                                .setWorkOrderId(orderId)
                                .setOpinion(opinion)
                                .build())
                        .build();
                Logger.d(message);
                WorkOrderResp response = null;
                try {
                    response = getStub(channel).processTask(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "processTask");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 回退
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, WorkOrderResp> backTask(
            String orderId,
            String opinion,
            CallBack<WorkOrderResp> callBack) {
        return new GrpcAsyncTask<String, Void, WorkOrderResp>(callBack) {
            @Override
            protected WorkOrderResp doRequestData(ManagedChannel channel) {
                AddWorkOrderEventReq message = AddWorkOrderEventReq.newBuilder()
                        .setWorkOrderEventDto(WorkOrderEventDto.newBuilder()
                                .setWorkOrderId(orderId)
                                .setOpinion(opinion)
                                .build())
                        .build();
                Logger.d(message);
                WorkOrderResp response = null;
                try {
                    response = getStub(channel).backTask(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "backTask");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 转派
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, WorkOrderResp> forwardTask(
            String orderId,
            String opinion,
            String assignUserName,
            String userId,
            CallBack<WorkOrderResp> callBack) {
        return new GrpcAsyncTask<String, Void, WorkOrderResp>(callBack) {
            @Override
            protected WorkOrderResp doRequestData(ManagedChannel channel) {
                AddWorkOrderEventReq message = AddWorkOrderEventReq.newBuilder()
                        .setWorkOrderEventDto(WorkOrderEventDto.newBuilder()
                                .setWorkOrderId(orderId)
                                .setOpinion(opinion)
                                .setAssignUserName(assignUserName)
                                .setAssignUserId(userId)
                                .build())
                        .build();
                Logger.d(message);
                WorkOrderResp response = null;
                try {
                    response = getStub(channel).forwardTask(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "forwardTask");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 挂起
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, WorkOrderResp> suspendTask(
            String orderId,
            String opinion,
            long time,
            CallBack<WorkOrderResp> callBack) {
        return new GrpcAsyncTask<String, Void, WorkOrderResp>(callBack) {
            @Override
            protected WorkOrderResp doRequestData(ManagedChannel channel) {
                AddWorkOrderEventReq message = AddWorkOrderEventReq.newBuilder()
                        .setWorkOrderEventDto(WorkOrderEventDto
                                .newBuilder()
                                .setWorkOrderId(orderId)
                                .setOpinion(opinion)
                                .setHangEndTime(time)
                                .build())
                        .build();
                Logger.d(message);
                WorkOrderResp response = null;
                try {
                    response = getStub(channel).suspendTask(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "suspendTask");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 解挂
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, WorkOrderResp> resumeTask(
            String orderId,
            String opinion,
            CallBack<WorkOrderResp> callBack) {
        return new GrpcAsyncTask<String, Void, WorkOrderResp>(callBack) {
            @Override
            protected WorkOrderResp doRequestData(ManagedChannel channel) {
                AddWorkOrderEventReq message = AddWorkOrderEventReq.newBuilder()
                        .setWorkOrderEventDto(WorkOrderEventDto
                                .newBuilder()
                                .setWorkOrderId(orderId)
                                .setOpinion(opinion)
                                .build())
                        .build();
                Logger.d(message);
                WorkOrderResp response = null;
                try {
                    response = getStub(channel).resumeTask(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "resumeTask");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 状态工单数量统计
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, CountStatusQuantityResp> countStatusQuantity(CallBack<CountStatusQuantityResp> callBack) {
        return new GrpcAsyncTask<String, Void, CountStatusQuantityResp>(callBack) {
            @Override
            protected CountStatusQuantityResp doRequestData(ManagedChannel channel) {
                CountStatusQuantityReq message = CountStatusQuantityReq.newBuilder()
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                CountStatusQuantityResp response = null;
                try {
                    response = getStub(channel).countStatusQuantity(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "countStatusQuantity");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * //评价星星个数，服务时间指标
     * int32 star_number = 21;
     * //评价星星个数，服务质量指标
     * int32 star_number_quality = 22;
     * 工单评价
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, WorkOrderResp> evaluateTask(
            String workOrderId,
            int starNumber,
            int starNumberQuality,
            String opinion,
            CallBack<WorkOrderResp> callBack) {
        return new GrpcAsyncTask<String, Void, WorkOrderResp>(callBack) {
            @Override
            protected WorkOrderResp doRequestData(ManagedChannel channel) {
                AddWorkOrderEventReq message = AddWorkOrderEventReq.newBuilder()
                        .setWorkOrderEventDto(
                                WorkOrderEventDto.newBuilder()
                                        .setWorkOrderId(workOrderId)
                                        .setStarNumber(starNumber)
                                        .setStarNumberQuality(starNumberQuality)
                                        .setOpinion(opinion)
                                        .build())
                        .build();
                Logger.d(message);
                WorkOrderResp response = null;
                try {
                    response = getStub(channel).evaluateTask(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "evaluateTask");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }
}
